﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Wicresoft.UnifyShow2.Portal.Models;
using Microsoft.Practices.Unity;
using Wicresoft.UnifyShow.Business;
using Wicresoft.UnifyShow2.Portal.ViewModels;

namespace Wicresoft.UnifyShow2.Portal.Controllers
{
    [Authorize]
    public class AdminDeviceController : BaseController
    {
        //private IUser_X user;
        private IDevice device;
        private ISiteBranch siteBranch;
        private IBusinessSystem businesssytem;

        protected override void Dispose(bool disposing)
        {
            //((IDisposable)user).Dispose();
            ((IDisposable)device).Dispose();
            ((IDisposable)siteBranch).Dispose();
            ((IDisposable)businesssytem).Dispose();
        }

        [InjectionConstructor]
        public AdminDeviceController(/*IUser_X user, */IDevice device, ISiteBranch siteBranch, IBusinessSystem businesssytem)
        {
            //this.user = user;
            this.device = device;
            this.siteBranch = siteBranch;
            this.businesssytem = businesssytem;
        }

        //
        // GET: /AdminDevice/

        public ActionResult Add()
        {
            return PartialView(new DeviceAndTypes());
        }

        [HttpPost]
        [ActionName("Add")]
        public ActionResult AddA()
        {
            DeviceModel model = new DeviceModel();
            TryUpdateModel<DeviceModel>(model, "Device");
            if (device.GetIsExist(model))
            {
                return JavaScript("alert('此设备已存在');");
            }
            else if (device.GetIsExistNoBSID(model))
            {
                return JavaScript("alert('此设备已从监控系统同步过来，请在《未相关业务系统的设备列表》列表查找，并相关。');");
            }
            else
            {
                model.CreateTime = DateTime.Now;
                device.Add(model);
                string GJ_IPPort2 = siteBranch.GetSiteBranchByID(int.Parse(model.SiteId.ToString())).GJ_IPPort2;
                string Name = businesssytem.GetBusinessSystemByID(int.Parse(model.BusinessSystemId.ToString())).Alias;
                SyncHelper.SyncAddDevice(new DeviceSyncModel()
                {
                    GJ_IPPort2 = GJ_IPPort2,
                    Identify = model.Identify,
                    Name = Name
                });
                return new EmptyResult();
            }
        }

        public ActionResult Edit(int deviceID)
        {
            return PartialView(new DeviceAndTypes() { Device = device.GetDeviceByID(deviceID) });
        }

        [HttpPost]
        public void Edit()
        {
            DeviceModel model = new DeviceModel();
            TryUpdateModel<DeviceModel>(model, "Device");
            device.Update(model);
        }

        [HttpPost]
        public void Delete(int id)
        {
            DeviceSyncModel sync = device.GetDeviceSyncModel(id);
            device.InitBsIdByDeviceId(id);
            //device.Delete(id);
            SyncHelper.SyncDeleteDevice(sync);
        }

        public ActionResult List(int bsID, string identify = "", string type = "", int page = 1, string sort = "CreateTime", string sortDir = "Descending")
        {
            const int pageSize = 10;
            decimal totalCount;
            var devices= device.GetDevicePagedByBSID(bsID, out totalCount, identify, type, pageSize, page, sort, sortDir);
            PagedDeviceModel pdm = new PagedDeviceModel() { Devices = devices, PageNumber = page, PageSize = pageSize, TotalRows = totalCount };
            return PartialView(pdm);
        }

        public ActionResult NoBSIDList(int bsID)
        {
            return PartialView(device.GetNoBSIDDeviceByBSID(bsID));
        }

        [HttpPost]
        public void SubmitNOBSIDDevice(int deviceID, int siteID, int businessSystemID, string identify)
        {
            string GJ_IPPort2 = siteBranch.GetSiteBranchByID(siteID).GJ_IPPort2;
            string Name = businesssytem.GetBusinessSystemByID(businessSystemID).Alias;
            SyncHelper.SyncAddDevice(new DeviceSyncModel()
            {
                GJ_IPPort2 = GJ_IPPort2,
                Identify = identify,
                Name = Name
            });
            device.UpdateNoBSID(new DeviceModel()
            {
                BusinessSystemId = businessSystemID,
                ID = deviceID
            });
        }
    }
}
